package com.asr.testclient.view;

import javax.swing.*;
import java.awt.*;

/**
 * 连接控制面板
 * 包含服务器地址、端口输入和连接按钮
 */
public class ConnectionPanel extends JPanel {
    private JTextField serverAddressField;
    private JTextField portField;
    private JButton connectButton;
    private JButton disconnectButton;
    private JLabel statusLabel;

    public ConnectionPanel() {
        initComponents();
        layoutComponents();
    }

    private void initComponents() {
        serverAddressField = new JTextField("localhost", 15);
        portField = new JTextField("8888", 6);
        connectButton = new JButton("连接");
        disconnectButton = new JButton("断开");
        disconnectButton.setEnabled(false);
        statusLabel = new JLabel("未连接");
        statusLabel.setForeground(Color.GRAY);
    }

    private void layoutComponents() {
        setLayout(new GridBagLayout());
        setBorder(BorderFactory.createTitledBorder("服务器连接"));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 服务器地址标签
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        add(new JLabel("服务器地址:"), gbc);

        // 服务器地址输入框
        gbc.gridx = 1;
        gbc.weightx = 1.0;
        add(serverAddressField, gbc);

        // 端口标签
        gbc.gridx = 2;
        gbc.weightx = 0;
        add(new JLabel("端口:"), gbc);

        // 端口输入框
        gbc.gridx = 3;
        gbc.weightx = 0.3;
        add(portField, gbc);

        // 连接按钮
        gbc.gridx = 4;
        gbc.weightx = 0;
        add(connectButton, gbc);

        // 断开按钮
        gbc.gridx = 5;
        add(disconnectButton, gbc);

        // 状态标签
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 6;
        gbc.weightx = 1.0;
        add(statusLabel, gbc);
    }

    /**
     * 获取服务器地址
     */
    public String getServerAddress() {
        return serverAddressField.getText().trim();
    }

    /**
     * 获取端口号
     */
    public int getPort() {
        try {
            return Integer.parseInt(portField.getText().trim());
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    /**
     * 设置连接状态显示
     */
    public void setConnectionStatus(String status) {
        statusLabel.setText(status);
        
        // 根据状态设置颜色
        if (status.contains("已连接")) {
            statusLabel.setForeground(new Color(0, 128, 0));
        } else if (status.contains("失败") || status.contains("错误") || status.contains("断开")) {
            statusLabel.setForeground(Color.RED);
        } else if (status.contains("连接中")) {
            statusLabel.setForeground(Color.BLUE);
        } else {
            statusLabel.setForeground(Color.GRAY);
        }
    }

    /**
     * 设置连接状态，启用/禁用相关控件
     */
    public void setConnected(boolean connected) {
        serverAddressField.setEnabled(!connected);
        portField.setEnabled(!connected);
        connectButton.setEnabled(!connected);
        disconnectButton.setEnabled(connected);
    }

    /**
     * 获取连接按钮
     */
    public JButton getConnectButton() {
        return connectButton;
    }

    /**
     * 获取断开按钮
     */
    public JButton getDisconnectButton() {
        return disconnectButton;
    }
}
